package com.godGuang.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.godGuang.common.core.domain.PageResult;
import com.godGuang.common.core.domain.Result;
import com.godGuang.common.core.utils.StringUtils;
import com.godGuang.domain.OssFile;
import com.godGuang.domain.Video;
import com.godGuang.domain.request.RequestVideo;
import com.godGuang.domain.request.VideoRequest;
import com.godGuang.servser.HealthVideoService;
import com.godGuang.util.OssUtill;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * @author : 段敏强
 * @className : HealthVideoController
 * @description : 描述说明该类的功能
 * @createTime : 2024/01/16 09:15
 */

/**
 * 健康视频Controller接口
 */


@RestController
@RequestMapping("/healthVideo")
@Log4j2
public class HealthVideoController {

    @Autowired
    private HealthVideoService healthVideoService;

    @Autowired
    private OssUtill ossUtill;

    /**
     * 上传图片并返回图片链接
     *
     * @param file 待上传的文件
     * @return 上传成功后的图片链接
     * @throws IOException 文件上传过程中可能出现的IO异常
     */
    @PostMapping("/uploadImage")
    public Result uploadImage(MultipartFile file) throws IOException {
        // 上传文件
        OssFile fileName = ossUtill.uploadFile(file, "5");
        // 返回文件链接
        String url = fileName.getFileName();
        // 返回结果
        Result result = new Result<>();
        // 返回数据
        HashMap<String, String> map = new HashMap<>();
        map.put("url", url);
        map.put("fileName", url);
        map.put("new FileName", url);
        map.put("originalFilename", file.getOriginalFilename());
        result.setData(fileName.getFileUrl());
        return result;
    }


    /**
     * 添加健康视频
     *
     * @param video 视频对象
     * @return 返回操作结果
     */
    @PostMapping("/healthVideoAdd")
    public Result healthVideoAdd(@RequestBody Video video) {
        log.info("添加视频:{}", video);
        if (StringUtils.isBlank(video.getVideoAddress())) {
            throw new RuntimeException("视频真在上传中");
        }
        // 添加视频
        healthVideoService.healthVideoAdd(video);
        log.info("添加视频成功");
        ;
        // 返回结果
        return Result.success();
    }


    /**
     * 获取健康视频列表
     *
     * @param request 查询参数
     * @return 返回分页结果
     */
    @PostMapping("/healthVideoList")
    public Result<PageResult<Video>> healthVideoList(@RequestBody RequestVideo request) {
        log.info("查询视频列表:{}", request);
        // 分页查询
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        // 查询视频列表
        List<Video> list = healthVideoService.healthVideoList(request.getVideoName());
        PageInfo<Video> videoPageInfo = new PageInfo<>(list);
        for (Video video : list) {
            VideoRequest videoRequest = new VideoRequest();
            videoRequest.setVideoId(video.getVideoId());
            videoRequest.setUserId(request.getUserId());
            List<Video> videos = healthVideoService.preferenceById(videoRequest);
            if (list.size() == 0) {
                video.setStatus(0);
                return PageResult.toResult(videoPageInfo.getTotal(), list);
            }
            if (1 >= videos.size()) {
                video.setStatus(1);
                return PageResult.toResult(videoPageInfo.getTotal(), list);
            }
        }
        // 返回结果
        return PageResult.toResult(videoPageInfo.getTotal(), list);
    }


    @PostMapping("/healthVideoById")
    public Result<List<Video>> healthVideoById(@RequestBody VideoRequest videoRequest) {
        log.info("查询视频:{}", videoRequest);
        // 查询视频
        List<Video> video = healthVideoService.healthVideoById(videoRequest);
        // 返回结果
        return Result.success(video);
    }


    @PostMapping("/healthVideoDelete/{videoId}")
    public Result healthVideoDelete(@PathVariable String videoId) {
        log.info("删除视频:{}", videoId);
        healthVideoService.healthVideoDelete(videoId);
        log.info("删除视频成功");
        return Result.success();
    }


    @PostMapping("/preferenceAdd")
    public Result preferenceAdd(@RequestBody VideoRequest videoRequest) {
        log.info("添加视频:{}", videoRequest);
        List<Video> videos = healthVideoService.preferenceById(videoRequest);
        if (videos.size() > 0) {
            healthVideoService.preferenceDelete(videoRequest);
            return Result.error();
        }

        healthVideoService.preferenceAdd(videoRequest);
        log.info("添加视频成功");
        return Result.success();
    }


    @PostMapping("/preferenceList/{userId}")
    public Result<List<Video>> preferenceDelete(@PathVariable String userId) {
        log.info("查询视频:{}", userId);
        List<Video> video = healthVideoService.preferenceList(userId);
        log.info("查询视频成功");
        return Result.success(video);
    }


}
